package ru.jexp.rcp.supply.beans.nakl;

import java.util.Date;
import java.util.List;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;

import ru.jexp.kontragent.Kontragent;
import ru.jexp.rcp.supply.actions.IActionNewWizard;
import ru.jexp.rcp.supply.actions.wizards.ActionNewWizardNakl;
import ru.jexp.rcp.supply.beans.account.WizardPageDocParams;
import ru.jexp.rcp.supply.beans.account.WizardPageDocStr;
import ru.jexp.rcp.supply.editor.EditorInputBean;
import ru.jexp.rcp.supply.editor.EditorPageNakl;
import ru.jexp.rcp.supply.mui.JExpSupplyMessages;
import ru.jexp.rcp.supply.wizard.JExpWizardListener;
import ru.jexp.ucet.nakladnaa.Nakladnaa;

public class NewWizardNakl extends Wizard implements IActionNewWizard,
		INewWizard, IWizardNakl {

	public static final String ID = "ru.jexp.rcp.supply.beans.nakl.newwizardnakl";

	public static final int WIZARD_PAGES_CREATED = 1;

	public static final int PERFORM_FINISH = 2;

	private IWorkbenchWindow window;

	private ListenerList listeners = new ListenerList();

	private WizardPageDocStr<StrokaNakladnoiWrap> pageDetail;

	private WizardPageDocStr<StrokaNakladnoiWrap> pageMaterial;

	private WizardPageDocStr<StrokaNakladnoiWrap> pagePki;

	@Override
	public boolean performFinish() {
		fireWizardListeners(NewWizardNakl.PERFORM_FINISH);
		return true;
	}

	@Override
	public boolean canFinish() {
		return super.canFinish();
	}

	@Override
	public void addPages() {
		pageDetail = WizardPageDocStr.<StrokaNakladnoiWrap> createDetail();
		pageMaterial = WizardPageDocStr.<StrokaNakladnoiWrap> createMaterial();
		pagePki = WizardPageDocStr.<StrokaNakladnoiWrap> createPki();

		addPage(new WizardPageDocParams(
				JExpSupplyMessages.Nakl_wizard_params_pagename));
		addPage(pageDetail);
		addPage(pageMaterial);
		addPage(pagePki);

	}

	@Override
	public IAction getAction() {
		return new ActionNewWizardNakl(window);
	}

	@Override
	public void init(IWorkbenchWindow window) {
		this.window = window;
		setWindowTitle(JExpSupplyMessages.Nakl_wizard_title);
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		init(workbench.getActiveWorkbenchWindow());
		new NewWizardNaklPresentor(this);
	}

	public void fireWizardListeners(int typeEvent) {
		Object[] copyListeners = listeners.getListeners();
		for (int i = 0; i < copyListeners.length; i++) {
			JExpWizardListener listener = (JExpWizardListener) copyListeners[i];
			listener.event(typeEvent);
		}
	}

	@Override
	public void addWizardListener(JExpWizardListener listener) {
		listeners.add(listener);
	}

	@Override
	public void removeWizardListener(JExpWizardListener listener) {
		listeners.add(listener);
	}

	@Override
	public Date getDate() {
		return getPageParams().getDate();
	}

	@Override
	public String getNumber() {
		return getPageParams().getNumber();
	}

	@Override
	public Kontragent getSupplier() {
		return getPageParams().getSuppler();
	}

	@Override
	public void setActivePageMessage(String message) {
		WizardPage currentPage = (WizardPage) getContainer().getCurrentPage();
		currentPage.setMessage(message, IMessageProvider.WARNING);
		currentPage.setPageComplete(message == null);
		getContainer().updateButtons();
	}

	public WizardPageDocParams getPageParams() {
		return (WizardPageDocParams) getPage(JExpSupplyMessages.Nakl_wizard_params_pagename);
	}

	@Override
	public void setInputDetailPage(List<StrokaNakladnoiWrap> listDetail) {
		pageDetail.setInputDocStr(listDetail);
	}

	@Override
	public void setInputMaterialPage(List<StrokaNakladnoiWrap> listMaterial) {
		pageMaterial.setInputDocStr(listMaterial);
	}

	@Override
	public void setInputPkiPage(List<StrokaNakladnoiWrap> listPki) {
		pagePki.setInputDocStr(listPki);
	}

	@Override
	public void openEditor(Nakladnaa nakl) {
		EditorInputBean input = new EditorInputBean(
				JExpSupplyMessages.MultiNew_action_nakl_title, nakl);
		try {
			window.getActivePage().openEditor(input, EditorPageNakl.ID);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<StrokaNakladnoiWrap> getCheckedListDetail() {
		return pageDetail.getCheckedElements();
	}

	@Override
	public List<StrokaNakladnoiWrap> getCheckedListMaterial() {
		return pageMaterial.getCheckedElements();
	}

	@Override
	public List<StrokaNakladnoiWrap> getCheckedListPki() {
		return pagePki.getCheckedElements();
	}
}
